package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.*;
import com.jiahao.material.services.IAdminService;
import com.jiahao.material.services.IRoleService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    /**
     * 添加仓库人员
     * @param admin
     * @return
     * @throws Exception
     */
    @RequiresPermissions("addAdmin")
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) throws Exception{
        admin.setStatus("01");
        adminService.save(admin);
        return ResultUtil.success("添加仓库人员成功!");
    }

    /**
     * 根据id查询人员
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Admin byId = adminService.getById(id);
        return ResultUtil.success("",byId);
    }

    /**
     * 获取所有已激活的仓库人员
     * @return
     * @throws Exception
     */
    @GetMapping("/activeAll")
    public Result getActiveAll() throws Exception{
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("status","01");
        List<Admin> list = adminService.list(adminQueryWrapper);
        return ResultUtil.success("",list);
    }

    /**
     * 分页查询
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Admin> page = new Page<>(currentPage,pageSize);

        //设置查询条件
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        if (map.get("name")!=null && !map.get("name").toString().equals("")){
            adminQueryWrapper.like("name",map.get("name").toString());
        }

        if (map.get("phone")!=null && !map.get("phone").toString().equals("")){
            adminQueryWrapper.like("phone",map.get("phone").toString());
        }

        if (map.get("sex")!=null && !map.get("sex").toString().equals("")){
            adminQueryWrapper.eq("sex",map.get("sex").toString());
        }

        if (map.get("status")!=null && !map.get("status").toString().equals("")){
            adminQueryWrapper.eq("status",map.get("status").toString());
        }

        IPage<Admin> page1 = adminService.page(page, adminQueryWrapper);
        return ResultUtil.success("",page1);
    }


    /**
     * 根据id更新仓库人员
     * @param admin
     * @return
     * @throws Exception
     */
    @RequiresPermissions("updateAdmin")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) throws Exception{
        adminService.updateById(admin);
        return ResultUtil.success("更新仓库人员成功!");
    }

    /**
     * 更新状态
     * @param map
     * @return
     * @throws Exception
     */
    @RequiresPermissions("updateAdmin")
    @PutMapping("/status")
    @Transactional
    public Result updateStatus(@RequestBody Map<String,Object> map) throws Exception{
        UpdateWrapper<Admin> adminUpdateWrapper = new UpdateWrapper<>();
        adminUpdateWrapper.eq("id",map.get("id").toString()).set("status",map.get("status").toString());
        adminService.update(adminUpdateWrapper);
        return ResultUtil.success("更新状态成功!");
    }

    @RequiresPermissions("delAdmin")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable String id) throws Exception{
        adminService.removeById(id);
        return ResultUtil.success("删除仓库人员成功!");
    }

    @RequiresPermissions("delAdmin")
    @PostMapping("/dels")
    public Result delByIds(@RequestBody List<String> ids) throws Exception{
        adminService.removeByIds(ids);
        return ResultUtil.success("删除仓库人员成功!");
    }

}
